/*
 * @Copyright: 2016 www.yyfax.com Inc. All rights reserved.
 */
package com.jaws.core.common.page;

import java.util.Collections;
import java.util.List;

import com.jaws.core.common.util.JacksonUtil;

/**
 * 分页
 * 
 * @author jiangmy
 * @date 2016-11-23 17:59:47
 * @since v1.0.0
 */
@SuppressWarnings("serial")
public class Page<PO>{

	// 页码,从1开始
	protected int pageNo = 1;
	// 每页记录数
	protected int pageSize = 10;
	// 总记录数
	protected int totalSize = 0;
	// 数据结果集合
	protected List<PO> dataList;

	/**
	 * 构造函数
	 */
	public Page() {
	}

	/**
	 * 构造函数
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param totalSize
	 */
	public Page(int pageNo, int pageSize, int totalSize) {
		super();
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.totalSize = totalSize;
	}

	/**
	 * 构造函数
	 * 
	 * @param pageSize
	 * @param totalSize
	 * @param dataList
	 */
	public Page(int pageNo, int pageSize, int totalSize, List<PO> dataList) {
		super();
		this.pageSize = pageSize;
		this.totalSize = totalSize;
		this.dataList = dataList;
		this.pageNo = pageNo < this.getTotalPage() ? pageNo : this.getTotalPage();
	}

	public int getPageNo() {
		return pageNo < 1 || pageNo > getTotalPage() ? 1 : pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize < 1 ? 10 : pageSize;
	}

	public int getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	public int getTotalPage() {
		return (getTotalSize() - 1) / getPageSize() + 1;
	}

	@SuppressWarnings("unchecked")
	public List<PO> getDataList() {
		return dataList == null ? Collections.EMPTY_LIST: dataList;
	}

	public void setDataList(List<PO> dataList) {
		this.dataList = dataList;
	}

	public int getOffset() {
		return this.pageSize * (this.getPageNo() - 1);
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public String toString() {
		return JacksonUtil.toJson(this);
	}

}
